package jdos.hardware;

import jdos.gui.Main;
import jdos.misc.Log;
import jdos.types.LogSeverities;
import jdos.types.LogTypes;

public class VGA_dac {

    public static short pel_index;
    public static short state;
    public static short write_index;
    public static short read_index;
    

    static private IoHandler.IO_WriteHandler write_p3c7 = new IoHandler.IO_WriteHandler() {
        public void call(/*Bitu*/int port, /*Bitu*/int val, /*Bitu*/int iolen) {
            read_index=(short)(val & 0xFF);
            pel_index=0;
            write_index= (short)((val + 1) & 0xFF);
        }
    };

    static private IoHandler.IO_WriteHandler write_p3c8 = new IoHandler.IO_WriteHandler() {
        public void call(/*Bitu*/int port, /*Bitu*/int val, /*Bitu*/int iolen) {
            write_index=(short)(val & 0xFF);
            pel_index=0;
        }
    };

    static private IoHandler.IO_ReadHandler read_p3c8 = new IoHandler.IO_ReadHandler() {
        public /*Bitu*/int call(/*Bitu*/int port, /*Bitu*/int iolen) {
            return write_index;
        }
    };

    static public /*Bitu*/int GFX_GetRGB(/*Bit8u*/int red,/*Bit8u*/int green,/*Bit8u*/int blue) {
		return ((blue << 0) | (green << 8) | (red << 16)) | (255 << 24);	
    }

    static int red;
    static int green;
    static int blue;
    static int[] redList = new int[256];
    static int[] greenList = new int[256];
    static int[] blueList = new int[256];

	public static void setPal(int index, int r, int g, int b) {
		Main.cmap[index] = GFX_GetRGB((r << 2) | (r >> 4), (g << 2) | (g >> 4), (b << 2) | (b >> 4));
	}

    static private IoHandler.IO_WriteHandler write_p3c9 = new IoHandler.IO_WriteHandler() {
        public void call(/*Bitu*/int port, /*Bitu*/int val, /*Bitu*/int iolen) {
            val&=0x3f;
            switch (pel_index) {
            case 0:
                red=val;
                redList[write_index] = val;
                pel_index=1;
                break;
            case 1:
                green=val;
                greenList[write_index] = val;
                pel_index=2;
                break;
            case 2:
                blue=val;
                blueList[write_index] = val;
                Main.cmap[write_index] = GFX_GetRGB((red << 2) | ( red >> 4 ), (green << 2) | ( green >> 4 ), (blue << 2) | ( blue >> 4 ));
                write_index++;
                write_index&=0xFF;
                pel_index=0;
                break;
            default:
                Log.log(LogTypes.LOG_VGAGFX,LogSeverities.LOG_NORMAL,"VGA:DAC:Illegal Pel Index");			//If this can actually happen that will be the day
                break;
            }
        }
    };
    static private IoHandler.IO_ReadHandler read_p3c9 = new IoHandler.IO_ReadHandler() {
        public /*Bitu*/int call(/*Bitu*/int port, /*Bitu*/int iolen) {
			int ret;
			switch (pel_index) {
			case 0:
				ret = redList[read_index];
				pel_index = 1;
				break;
			case 1:
				ret = 4;//green;
				ret = greenList[read_index];
				pel_index = 2;
				break;
			case 2:
				ret = 5;//blue;
				ret = blueList[read_index];
				read_index++;
				read_index &= 0xFF;
                pel_index=0;
    //		VGA.vga.dac.write_index=VGA.vga.dac.read_index+1;//disabled as it breaks wari
                break;
            default:
                Log.log(LogTypes.LOG_VGAMISC,LogSeverities.LOG_NORMAL,"VGA:DAC:Illegal Pel Index");			//If this can actually happen that will be the day
                ret=0;
                break;
            }
            return ret;
        }
    };

    static public void VGA_SetupDAC() {
        pel_index=0;
        write_index=0;
        read_index = 0;
        //if (Dosbox.IS_VGA_ARCH()) {
        /* Setup the DAC IO port Handlers */
        IoHandler.IO_RegisterWriteHandler(0x3c7,write_p3c7,IoHandler.IO_MB);
        IoHandler.IO_RegisterWriteHandler(0x3c8,write_p3c8,IoHandler.IO_MB);
        IoHandler.IO_RegisterReadHandler(0x3c8,read_p3c8,IoHandler.IO_MB);
        IoHandler.IO_RegisterWriteHandler(0x3c9,write_p3c9,IoHandler.IO_MB);
        IoHandler.IO_RegisterReadHandler(0x3c9,read_p3c9,IoHandler.IO_MB);
        //}
    }
}
